/*
 * ringtonemaker.h
 * Supporting code to make morse code ringtones
 * Created 20120510
 * Last modified 20120512
 */

#ifndef HAVE_RINGTONEMAKER_H
#define HAVE_RINGTONEMAKER_H

#include <stdint.h>

#define NCHANNELS 1 /* Mono */
#define RISETIME 5 /* Time, in msec for keying envelope */
#define WAVE_FORMAT_PCM 0x0001

struct wav_header {
    uint32_t ckID; /* "RIFF" */
    uint32_t cksize;
    uint32_t WAVEID; /* "WAVE" */
    struct {
        uint32_t ckID; /* "fmt " */
        uint32_t cksize;
        uint16_t wFormatTag;
        uint16_t nChannels;
        uint32_t nSamplesPerSec;
        uint32_t nAvgBytesPerSec;
        uint16_t nBlockAlign;
        uint16_t wBitsPerSample;
    } fmt;
    struct {
        uint32_t ckID;
        uint32_t cksize;
    } data;
};

/* Structure containing user's options */
/* Don't forget to update parse_opts and usage */
struct {
    /* Audio Frequency */
    int afreq;
    /* Loudness */
    int loudness;
    /* Sample Rate */
    int srate;
    /* Sample Size (1 or 2 bytes) */
    int ssize;
    /* Code Speed */
    int cspeed;
    /* File prefix */
    char *fprefix;
    /* Add a morse code space at the end of the code */
    int addspace;
    /* Be verbose? */
    int verbose;
    
    /* Non option data to keep to avoid recalculation */
    float ditlen; /* length of a dit in milliseconds */
    float amplitude; /* Amplitude of the signal */
} optstruct;

/* Pointer to the name of the executable */
char *exname;

/* Building blocks */
int16_t *dit, *dah, *elspace, *letterspace, *wordspace; /* Sound chunks */
int ndit, ndah, nelspace, nletterspace, nwordspace; /* Number of samples */

/* Function headers */
int parse_opts(int argc, char **argv);
/* Parse/store an integer option */
int parseintopt(char *intasstr, int *dst, const char *errmsg);
/* Print a usage statement */
void usage();
/* Print a TODO string */
void TODO(char *todostring);
/* Print config options */
void print_config();
/* Load the header.  optstruct *must* be populated before this is called. */
void load_header(struct wav_header *header);
/* Update the header with a known number of samples */
void update_header_with_nsamples(int nsamples, struct wav_header *header);
/* Get the string to turn into code.  Returns allocated memory */
char* getcodestr(int argc, char **argv, char **fname);
/* Make a code element */
int16_t* make_element(int len, int *count);
/* Makes spaces */
int16_t* make_space(int len, int *count);
/* Print the header */
void print_header(struct wav_header *header);
/* Count how many dits in a string */
int count_dits(char* str);
/* Write morse to the file. */
int write_morse(char *str, int fd);

#endif

